package com.banciyuan.bcywebview.base.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

/**
 * Created by hfxt2008 on 14-8-22.
 * 自定义progressdialog，没怎么在用，因为不太好统一格式，请优先考虑系统的alertdialog。
 */
public class MyProgressDialog extends Dialog {

	public MyProgressDialog(Context context) {
		super(context);
	}

	public MyProgressDialog(Context context, int theme) {
		super(context, theme);
	}

	public MyProgressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public static class Builder {
		private Context context;

		private TextView cancelBtn;
		private String cancelTxt;
		private RelativeLayout dialogLayout;

		private DialogInterface.OnClickListener cancelBtnClickListener;
		private DialogInterface.OnCancelListener onCancelListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setCancelTxt(String txt) {
			this.cancelTxt = txt;
			return this;
		}

		public Builder setCancelButton(DialogInterface.OnClickListener listener) {
			this.cancelBtnClickListener = listener;
			return this;
		}

		public Builder setOnCancelButton(DialogInterface.OnCancelListener listener) {
			this.onCancelListener = listener;
			return this;
		}

		public MyProgressDialog create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final MyProgressDialog mydialog = new MyProgressDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.write_dialog_layout, null);
			cancelBtn = ((TextView) layout.findViewById(R.id.myprogressdialog_cancel_btn));
			dialogLayout = ((RelativeLayout) layout.findViewById(R.id.dialoglayout));

			dialogLayout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
				}
			});
			cancelBtn.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					cancelBtnClickListener.onClick(mydialog, DialogInterface.BUTTON_NEGATIVE);
					//					cancelBtn.setClickable(false);
					mydialog.dismiss();
				}
			});
			if(!TextUtils.isEmpty(cancelTxt)) cancelBtn.setText(cancelTxt);
			mydialog.setCanceledOnTouchOutside(false);
			mydialog.setOnCancelListener(onCancelListener);
			mydialog.setContentView(layout);
			return mydialog;
		}
	}
}